package com.jinbiao.dao.config;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

/**
 * 配置文件   帮助实现配置信息
 * @author EX_WLJR_ZHUANGJB
 *
 */
@Configuration
public class MybatiesConfig {

	// 返回一个DataSources  数据资源
	@Bean
	@ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource(){
        return new org.apache.tomcat.jdbc.pool.DataSource();
    }
	
	// 返回一个sqlSessionFactory 工厂
	//不要忘了添加@Bean+ sqLsessionFactory进行注解
	@Bean(name = "sqlSessionFactory")
	public SqlSessionFactory sqlSessionFactoryBean() throws Exception{
		  SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
	        sqlSessionFactoryBean.setDataSource(dataSource());

	        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

	        //通过sqlSessionFactoryBean 得到mytabies。xml文件的资源  进行解析
	        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/*.xml"));
	        
	        return sqlSessionFactoryBean.getObject();
		
		
	}
	
}
